package com.pw.study.flink.watermark;

import com.pw.study.flink.entities.WaterSensor;
import com.pw.study.utils.AtguiguUtil;
import org.apache.flink.api.common.eventtime.SerializableTimestampAssigner;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.windowing.ProcessWindowFunction;
import org.apache.flink.streaming.api.windowing.assigners.TumblingEventTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;
import org.apache.flink.util.Collector;

import java.time.Duration;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

public class WatermarkStrategy<W> {

    public static void main(String[] args) {
        wc2();
    }

    private static void wc2() {
        Configuration conf = new Configuration();
        conf.setInteger("rest.port", 2000);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
        env.setParallelism(2);
        //1.构建数据
        SingleOutputStreamOperator<WaterSensor> stream = env
                .socketTextStream("hadoop111", 9999)
                .map(value -> {
                    String[] data = value.split(",");
                    return new WaterSensor(
                            data[0],
                            Long.valueOf(data[1]),
                            Integer.valueOf(data[2])
                    );
                });

        //2.构建策略
        //设置策略
        org.apache.flink.api.common.eventtime.WatermarkStrategy<WaterSensor> strategy = org.apache.flink.api.common.eventtime.WatermarkStrategy.forBoundedOutOfOrderness(Duration.ofSeconds(3));
        //设置自定义事件时间
        strategy.withTimestampAssigner((SerializableTimestampAssigner<WaterSensor>) (element, ts) -> element.getTs() * 1000);
        //设置防止数据倾斜
        strategy.withIdleness(Duration.ofSeconds(5));

        //3.处理数据
        stream
                .assignTimestampsAndWatermarks(strategy)
                .keyBy(WaterSensor::getId)
                .window(TumblingEventTimeWindows.of(Time.seconds(5)))
                .allowedLateness(Time.seconds(3))
                .process(new ProcessWindowFunction<WaterSensor, String, String, TimeWindow>() {
                    @Override
                    public void process(String key, Context ctx, Iterable<WaterSensor> elements, Collector<String> out) throws Exception {
                        List<WaterSensor> list = AtguiguUtil.toList(elements);
                        List<WaterSensor> list1 = StreamSupport.stream(elements.spliterator(), true).collect(Collectors.toList());
                        System.out.println("list: " + list1);
                        out.collect(key + "  " + ctx.window() + "  " + list);
                    }
                })
                .print();


        try {
            env.execute();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
